using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
namespace DIS_Client.MainManagers
{
	#region using
	using DevComponents.DotNetBar;
	using DIS_Client.Helpers;
	using System.Data;
	using System.Drawing;
	using System.IO;
	using DIS_Client.DIS_BUS_WS;
	#endregion
	public partial class FrmManageOrders : Office2007Form
	{
		public EventHandler Handler { get; set; }

		public FrmManageOrders()
		{
			InitializeComponent();
		}

		private void FrmManageOrders_Load(object sender, EventArgs e)
		{
			InitGridView();
			LoadData();
			dtgvData.ClearSelection();
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			FrmCreateOrders frm = new FrmCreateOrders();
			frm.Handler += LoadData;
			frm.ShowDialog();
		}

		private void btnDelete_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
                if (MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Confirm"), "Information", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
				{
					if (BusService.Instance.Orders_Delete(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString())))
					{
                        MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Successful"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
						LoadData();
					}
					else
					{
                        MessageBoxEx.Show(CommonHelper.GetMessage("Delete", "Fail"), "Information", MessageBoxButtons.OK, MessageBoxIcon.Error);
					}
				}
			}
		}

		private void btnUpdate_Click(object sender, EventArgs e)
		{
			if (dtgvData.SelectedRows.Count > 0)
			{
				FrmCreateOrders frm = new FrmCreateOrders(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString()));
				frm.Handler += LoadData;
				frm.ShowDialog();
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}

		private void InitGridView()
		{
			DataGridViewTextBoxColumn objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "ID",
				DataPropertyName = "ID",
				Name = "ID",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "Restaurant",
                DataPropertyName = "Restaurant",
                Name = "Restaurant",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "OrderDate",
				DataPropertyName = "OrderDate",
				Name = "OrderDate",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "IsUsed",
				DataPropertyName = "IsUsed",
				Name = "IsUsed",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5))
			};
			dtgvData.Columns.Add(objColumn);
			objColumn = new DataGridViewTextBoxColumn
			{
				HeaderText = "IsPaid",
				DataPropertyName = "IsPaid",
				Name = "IsPaid",
				Width = (int)((dtgvData.Width - dtgvData.RowHeadersWidth)*((double)1/5)) - 2
			};
			dtgvData.Columns.Add(objColumn);
		}

		private void LoadData(object sender, EventArgs e)
		{
			LoadData();
		}

		private void LoadData()
		{
			var lstItem = BusService.Instance.Orders_SelectByIDRestaurant(GlobalVariables.g_CurrentUser.IDRestaurant);
			var dt = new DataTable();
			dt.Columns.Add("ID");
            dt.Columns.Add("Restaurant");
			dt.Columns.Add("OrderDate");
			dt.Columns.Add("IsUsed");
			dt.Columns.Add("IsPaid");
			for (int i = 0; i < lstItem.Length; i++)
			{
				dt.Rows.Add(lstItem[i].ID, lstItem[i].Restaurant.Name, String.Format("{0:dd/MM/yyyy}", lstItem[i].OrderDate), lstItem[i].IsUsed == true ? "Yes" : "No", lstItem[i].IsPaid == true ? "Yes" : "No");
			}
			DataView objTimeSheetDV = new System.Data.DataView(dt);
			objTimeSheetDV.AllowNew = false;
			dtgvData.AutoGenerateColumns = false;
			dtgvData.DataSource = objTimeSheetDV;
			dtgvData.ClearSelection();
		}

        private void dtgvData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dtgvData.SelectedRows.Count > 0)
            {
                FrmCreateOrders frm = new FrmCreateOrders(int.Parse(dtgvData.SelectedRows[0].Cells["ID"].Value.ToString()));
                frm.Handler += LoadData;
                frm.ShowDialog();
            }
        }

	}
}